package com.carkeeper.serve.service.impl;



import com.carkeeper.serve.dao.ServeTypeMapper;
import com.carkeeper.serve.service.ServeTypeService;
import com.carkeeper.pojo.ServeType;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class ServeTypeServiceImpl implements ServeTypeService {

    @Autowired
    private ServeTypeMapper serveTypeMapper;


    /**
     * 分页条件查询
     * @param searchMap
     * @return
     */
    @Override
    public PageInfo<ServeType> findPage(Map<String,String> searchMap) {
        Example example = new Example(ServeType.class);
        Example.Criteria criteria = example.createCriteria();
        int pageNum = 1;
        int pageSize = 10;

        if (searchMap!=null){
            if (searchMap.get("parentId")!=null && !"".equals(searchMap.get("code"))){

                criteria.andEqualTo("parentId","parentId");
            }
            if (searchMap.get("startPage")!=null&&searchMap.get("pageSize")!=null){
                if (!"".equals(searchMap.get("startPage"))){
                    pageNum = Integer.parseInt(searchMap.get("startPage"));
                }
                if (!"".equals(searchMap.get("pageSize"))){
                    pageSize = Integer.parseInt(searchMap.get("pageSize"));
                }
            }

            if (searchMap.get("code")!=null&&!"".equals(searchMap.get("code"))){

                criteria.andEqualTo("code",searchMap.get("code"));
            }

            if (searchMap.get("name")!=null&&!"".equals(searchMap.get("name"))){

                criteria.andEqualTo("name",searchMap.get("name"));
            }

        }

        PageHelper.startPage(pageNum,pageSize);

        List<ServeType> serveTypeList = serveTypeMapper.selectByExample(example);
        PageInfo<ServeType> pageInfo = new PageInfo<>(serveTypeList);
        return pageInfo;
    }

    /**
     * 增加服务分类
     * @param serveType
     */
    @Override
    public void addServeType(ServeType serveType) {
        if (serveType==null){
            throw new RuntimeException("请输入要添加的服务分类信息");
        }
        serveTypeMapper.insert(serveType);
    }

    /**
     * 根据id查询服务分类信息
     * @return
     */
    @Override
    public ServeType findById(String id) {
        ServeType serveType = serveTypeMapper.selectByPrimaryKey(id);
        return serveType;
    }

    /**
     * 修改服务分类
     * @param serveType
     */
    @Override
    public void updateServeType(ServeType serveType) {
        serveTypeMapper.updateByPrimaryKeySelective(serveType);
    }

    /**
     * 删除服务分类
     * @param id
     */
    @Override
    public void deleteServeType(String id) {
        serveTypeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List findByLevel() {
        List list = serveTypeMapper.findByLevel();
        return list;
    }

}
